<?php
namespace app\admin\controller;
use think\Request;
use think\Db;
use  app\common\model\Report as ReportModel;
class Report extends \app\admin\controller\Base {

    protected $res = array(
        'index'   => array('css' => '2,3,4', 'js' => 'list,My97DatePicker'),
        'mold'   => array('css' => '2,3,4', 'js' => 'list'),
    );

    /***
     * @return mixed
     * 举报列表
     */
    public function index(){
        $ReportModel = new ReportModel();
        $search = input('get.search/a');
        $page = input('get.page');
        $search = $this->search($search, $page);
        $list = $ReportModel->relData(15,$search);
        $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
        if(!$page){
            $num = 0;
        }else{
            $num = $page-1;
        }
        $this->assign(
            array(
                'action'=>$action,
                'list'=>$list,
                'page'=>$page,
                'search' => $search,
                'num'=>$num
            )
        );
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function delIndex(){
        $ids = strtoarray(input('post.ids'));
        $where['report_id'] = array('In', $ids);
        $result = Db::table('up_report')->where($where)->delete();
        if ($result) {
            return $this->api_success('操作成功');
        } else {
            return $this->api_dbError();
        }
    }

    /***
     * @return mixed
     * 查看详情
     */
    public function detail(){
        $ReportModel = new ReportModel();
        $id = input('get.id');
        if(!$id){
            $this->api_dbError('参数传递错误');
        }
        $res = $ReportModel->findOne($id);
        $this->assign('res',$res);
        return $this->fetch();
    }

    /***
     * @return mixed
     * 审核页面
     */
    public function examine(){
        $ReportModel = new ReportModel();
        $id = input('get.id');
        if(!$id){
            $this->api_dbError('参数传递错误');
        }
        $res = $ReportModel->findOne($id);
        $this->assign('res',$res);
        return $this->fetch();
    }
    /***
     * @return \think\response\Json
     * 审核功能
     */
    public function examineInfo(){
        $ReportModel = new ReportModel();
        $id = input('post.id');
        $reason = input('post.reason');
        $status = input('post.status');
        $status_user = input('post.status_user');
        $data['report_id'] = $id;
        $data['report_start_time'] = date('Y-m-d H:i:s',time());
        $data['report_handle'] = session('admin')['id'];
        $data['report_status'] = 2;
        $report_user_id = Db::table('up_report')->where('report_id',$id)->value('report_user_id');
        $report_cover = Db::table('up_report')->where('report_id',$id)->find();
        $report_status = Db::table('up_u_user')->where('user_id',$report_cover['report_cover_user_id'])->value('user_status');
        if($report_status!=$status_user){
            Db::table('up_u_user')->where('user_id',$report_cover['report_cover_user_id'])->setField('user_status',$status_user);
        }
        if($status==2){
            $data['report_reason'] = $reason;
            $this->Jpush((string)$report_user_id,'您的提交的举报信息审核没有通过',1,array('type'=>1));
        }
        if($status==1){
            $this->Jpush((string)$report_user_id,'恭喜您的提交的举报信息审核通过',1,array('type'=>1));
        }
        $res = $ReportModel->update($data);
        if($res){
            return $this->api_success("审核成功");
        }else{
            return $this->api_dbError();
        }
    }
    /****
    /***
     * @return mixed
     * 举报类型
     */
    public function mold(){
        $action = '/' . Request::instance()->controller() . '/' . Request::instance()->action();
        $page = input('get.page');
        $list = Db::table('up_report_name')->paginate(15);
        if(!$page){
            $mun = 0;
        }else{
            $mun = $page-1;
        }
        $this->assign(
            array(
                'list'=>$list,
                'action'=>$action,
                'page'=>$page,
                'num'=>$mun
            )
        );
        return $this->fetch();
    }

    /**
     * @return mixed|\think\response\Json
     * 举报类型添加/编辑
     */
    public function saveReport(){
        if(Request::instance()->isPost()){
            $id = input('post.id');
            $reason_name = input('post.report_name');
            $report_order = input('post.report_order');
            if($id!=''){//证明是编辑
                $whereS['report_name'] = $reason_name;
                $whereS['report_type_id'] =array('NEQ',$id);
                $result = Db::table('up_report_name')->where($whereS)->find();
                if($result['report_type_id']!=''){
                    return $this->api_dbError("不能重复添加举报原因");
                }else{
                    $whereMap['report_type_id'] = $id;
                    $data['report_name'] =$reason_name;
                    $res = Db::table('up_report_name')->where($whereMap)->update($data);
                }
            }else{//添加
                $whereS['report_name'] = $reason_name;
                $result = Db::table('up_report_name')->where($whereS)->find();

                if($result['report_type_id']!=''){
                    return $this->api_dbError("不能重复添加举报原因");
                }else{
                    $data['report_name'] = $reason_name;
                    $data['report_order']=$report_order;
                    $res = Db::table('up_report_name')->insertGetId($data);
                }
            }
            if ($res) {
                return $this->api_success("操作成功");
            } else {
                return $this->api_dbError("操作失败");
            }
        }
        $id = input('get.id');
        if($id!=''){
            $where['report_type_id'] = $id;
            $list = Db::table('up_report_name')->where($where)->find();
            $this->assign('id', $id);
            $this->assign('ad', $list);
        }
        return $this->fetch();
    }

    /**
     * @return \think\response\Json
     * 删除举报原因
     */
    public function delReport(){
        $ids = strtoarray(input('post.ids'));
        $where['report_type_id'] = array('In', $ids);
        $result = Db::table('up_report_name')->where($where)->delete();
        if ($result) {
            return $this->api_success('操作成功');
        } else {
            return $this->api_dbError();
        }
    }

    /**
     * @return \think\response\Json
     * 排序
     */
    public function sortBanner(){
        $sort = input('post.sort');
        $wine_id = input('post.wine_id');
        $where['report_type_id'] = $wine_id;
        $res =  Db::table('up_report_name')->where($where)->setField("report_order",$sort);
        if ($res) {
            return $this->api_success("操作排序成功");
        } else {
            return $this->api_dbError("操作排序失败");
        }
    }

    /***
     * @return \think\response\Json
     */
    public function statusBanner(){
        $status = input('post.status');
        $act = input('post.act');
        $where['report_type_id'] = $act;
        $res = Db::table('up_report_name')->where($where)->setField("report_status",$status);
        if ($res) {
            return $this->api_success("操作成功");
        } else {
            return $this->api_dbError("操作失败");
        }
    }
}